<template>
  <div v-show="isNotAdd === 'index'">
    <!--页面搭建-杜思成-->
    <layout-card v-model="search" v-loading="searchLoading" :filters="ismapOptions" headline="信息列表" headtitle=" "
      :hasButton="true" @search-change="getData" @search-reset="searchReset">
      <template #action>
        <router-link to=""></router-link>
        <el-button type="primary" @click="handleCreate()">新建单位</el-button>
      </template>
      <table-common :height="tableHeight" :columns="mapListColumns" :loading="loading" :pagination="pager"
        :table-data="tabledata" size="large" @current-change="changeCurrent" @size-change="changePageSize"
        @sort-change="sortChange">
        <template #action="{ row }">
          <!-- 自定义操作栏示例  -->
          <el-button class="link-btn" @click="handleCheck(row)" link type="primary">查看</el-button>
          <el-button class="link-btn" @click="handleEdit(row)" link type="primary">编辑</el-button>
          <el-popconfirm title="是否删除?" confirmButtonText="确定" cancelButtonText="取消" @confirm="() => DeletRow(row)">
            <template #reference>
              <el-button class="link-btns" link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </table-common>
    </layout-card>
  </div>
  <unit-add @getValue="getUnitAddValue" :list="list" v-if="isNotAdd === 'add'" v-ref:edit></unit-add>
  <unit-edit @getValue="getUnitAddValue" ref="unitEditRef" :list="list" :data="data"
    v-show="isNotAdd === 'edit'"></unit-edit>
  <unit-check @getValue="getUnitAddValue" :data="data" v-if="isNotAdd === 'check'"></unit-check>
</template>

<script lang="ts" setup>
import { Search } from '@element-plus/icons-vue'

const radio = ref(0)
const input3 = ref('')
const select = ref('')
const isShow = ref(false)
const isNotAdd = ref('index')
const data = ref()
const list = ref()
const tableHeight = ref(window.innerHeight - 510)
/**
 * @radioChange 单选框的改变事件
 * @author
 * @date
 * @version V1.0.0
 * @return
 */
const radioChange = (value: number, number: boolean) => {
  // 选中自定义
  if (value == 1) {
    isShow.value = true
  } else { // 选中默认
    isShow.value = false
  }
  console.log(value)
  console.log(isShow.value)
}
import { computed, ref, reactive, onBeforeMount, nextTick } from "vue";
import LayoutCard from "@/components/LayoutCard/index.vue";
import TableCommon from "@/components/TableCommon/index.vue";
import {
  houseDeleteRow,
  houseImport,
  append,
  change,
  getUserInfo,
} from "@/api/eqpt/houseManger";
import { mapListColumns } from "@/views/secure/archives/unit/config/columns";
import {
  mapOptions,
  dialogOptions,
  mapListFilters,
} from "@/views/secure/archives/unit/config/filters";
import {
  ElMessage,

} from "element-plus";

import FilterBar from "@/components/FilterBar/index.vue";
import { getResource, postResource } from "@/api/resource";
import { securityUnitListPage, getCatalogueList, deleteSecurityUnit, getFormJson } from '@/api/secure/securityUnit'

import { useStore } from "vuex";
// 地图控件
import mapControl from "@/components/map/index.vue";
import { store } from "@/components/map/mapStore";
import Add from "@/views/construction/project-application/add.vue";
import UnitAdd from "@/views/secure/archives/unit/unitAdd.vue";
import UnitEdit from "@/views/secure/archives/unit/unitEdit.vue";
import UnitCheck from "@/views/secure/archives/unit/unitCheck.vue";
import unitAdd from "@/views/secure/archives/unit/unitAdd.vue";
import { da } from 'element-plus/es/locale'

const loading = ref(false);
const searchLoading = ref(false);
const stores = useStore();
const organTree = stores.state.user.organTree; // 获取树
const isdialogOptions = ref(dialogOptions);
const ismapOptions = ref(mapOptions);
const ismapListFilters = ref(mapListFilters);
const thresholdDialogVisible = ref(false);
const unitEditRef: any = ref(null);
const addTilte = ref(""); // 弹出框名称

// 初始化单位类型列表

const catalogueList = ref([])
const initCatalogueList = () => {
  // searchLoading.value = true;
  getCatalogueList().then((res: any) => {
    console.log('initCatalogueList')
    let data={
      label:'',
      value:''
    };
    console.log(res)
    catalogueList.value = [{
      label: '全部',
      value: null
    }]
    res.forEach(item => {
      let data={
      label:'',
      value:''
    };
      data.label=item.departName
      data.value=item.departName
      catalogueList.value.push(data);
    });
    ismapOptions.value[5].inputProps.data = catalogueList.value;
    console.log('ismapOptions.value[5].inputProps.data')
    console.log(ismapOptions.value[5].inputProps.data)
    searchLoading.value = false;
  })
}

// 初始化模板列表
const initUnitModel = () => {
  getFormJson().then((res: any) => {
    console.log('initUnitModel')
    console.log(res)
    // let data=[];
    // let dataindex=0;
    // res.forEach(item => {
    //   data[dataindex++] = item.id
    // });
    list.value = res;
  })
}
// 分页和数据展示参数
const current = ref(1);
const pageSize = ref(10);
const total = ref(0);
const tabledata = ref([]);
const search = ref({
  unitName: null,
  roomNumber: null,
  creatTime: null,
  unitNumber: null,
  headName: null,
  unitType: null,
});
const pager = computed(() => ({
  currentPage: current.value,
  pageSize: pageSize.value,
  total: total.value,
  pageCount:
    Math.ceil(total.value / pageSize.value) === 0
      ? 1
      : Math.ceil(total.value / pageSize.value),
}));

/**
 * @functionName 分页、查询、排序、删除功能
 * @author 杜思成
 * @date 2023-08-08 17:46:09
 * @version V1.0.0
 */
const changeCurrent = (value: number) => {
  current.value = value;
  getData()
};
const changePageSize = (size: number) => {
  pageSize.value = size;
  getData()
};
const order = ref([
  {
    column: "state",
    method: "asc",
  },
  {
    column: "lastUpdateTime",
    method: "desc",
  },
]);
/**
 * @functionName 获取数据
 * @author 彭庆楠
 * @date 2023-08-08 17:47:31
 * @version V1.0.0
 * @return
 */
const getData = async () => {
  // loading.value = true
  console.log(search.value);
  
  const params = {
    unitName: search.value?.unitName,
    unitNumber: search.value?.unitNumber,
    roomNumber: search.value?.roomNumber,
    headName: search.value?.headName,
    unitType: search.value?.unitType,
    recycle:'1'
    // startTime: search.value?.creatTime?.[0],
    // endTime: search.value?.creatTime?.[1],
  }
  const playout = {
    page: {
      page: current.value-1,
      size: pageSize.value
    }, 
    myTime2: {
      startTime: search.value?.creatTime?.[0],
      endTime: search.value?.creatTime?.[1],
    },
    externalUnit: params,

  };
  console.log(playout);
  
  securityUnitListPage(playout).then(res => {
    console.log(res.content);
      tabledata.value = res.content
      console.log(tabledata);
      total.value = res.totalElements
  })


};
/**
 * @functionName 排序事件
 * @param {any} data 传入的某一列的列名和升降序
 * @author 彭庆楠
 * @date 2023-08-17 17:48:41
 * @version V1.0.0
 * @return
 */
const sortChange = (data: any) => {
  const method = data.order === "ascending" ? "asc" : "desc";
  order.value[0].column = data.prop;
  order.value[0].method = method;
  getData();
};
// 删除
const DeletRow = (row: any) => {
  console.log(row)
  deleteSecurityUnit(row).then((res) => {
    if (res) {
      getData();
      ElMessage({
        message: "删除成功",
        type: "success",
      });
    } else {
      ElMessage.error("删除失败");
    }
  });
};

/**
 * addSave单位，isNotAdd--判断是否显示unitAdd组件
 */
const handleCreate = () => {
  addTilte.value = 'add'
  isNotAdd.value = 'add'
};

/**
 * 自定义事件，获取单位添加传递过来的数据，isNotAdd--判断是否显示unitAdd组件
 */
const getUnitAddValue = (val: any) => {
  isNotAdd.value = val
  console.log(isNotAdd.value);
  if (val === 'index') {
    getData()
  }
};
/**
 * 查看按钮
 */
const handleCheck = (row: any) => {
  addTilte.value = 'check'
  data.value = row
  isNotAdd.value = 'check'
}
/**
 * 编辑按钮
 * @param row
 */
const handleEdit = async (row: any) => {
  addTilte.value = 'edit'
  data.value = row
  console.log(data.value)
  isNotAdd.value = 'edit'
};

const FilterRef = ref({});

/**
 * @functionName 重置功能
 * @param {FilterDataType} data
 * @description
 * @author 杜思成
 * @date 2023-08-17 17:55:57
 * @version V1.0.0
 * @return
 */
const searchReset = (data: FilterDataType) => {
  search.value = data;
  pager.value.currentPage = 1;
  pager.value.pageSize = 10;
  getData();
};

onBeforeMount(() => {
  initCatalogueList()
  initUnitModel()
  getData()
});
</script>
<style scoped>
.form {
  .el-input {
    width: 150px;
  }
}

.link-btn {
  color: #2F7DF1;
}

.link-btns {
  color: #F24A45;
}
</style>
